<#assign classDesc = "控制器">
<#include "/java_copyright.include">
package ${controller};

import ${basepackage}.core.annotation.BusinessDesc;
import ${basepackage}.core.vo.JsonResult;
import ${basepackage}.core.vo.QueryParams;
import ${basepackage}.core.vo.ResultCode;
import ${basepackage}.core.vo.inf.IResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import ${iservice}.I${className}Service;
import ${vo}.${className};
import ${project}.web.controller.BaseController;
import java.util.List;

<#include "/java_comments.include">
@Controller
@RequestMapping("/${table.urlPre}")
public class ${className}Controller extends BaseController {
    //菜单编码
    private static final String menuCode = "${table.permissionCode}";
    //模块描述
    private static final String MODULE_DESC = "${table.functionName}";
    @Autowired
    private I${className}Service ${classNameLower}Service;
    @Override
    protected String getViewDir() {
        return "${page_dir}/";
    }
    
    /**
     * 去查询${table.functionName}
     * 
     * @param ${classNameLower} ${table.functionName}页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author ${table.codeAuthor}
     * @since [${table.codeVersion}]
     * @version [${table.codeVersion}, ${now?string('yyyy-MM-dd')}]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询${table.functionName}列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "tree", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView tree(${className} ${classNameLower}) {
        ModelAndView mav = new ModelAndView();
        try {
            mav.setViewName(getView("${classNameLowerCase}_tree"));
            mav.addObject("searchEntity", ${classNameLower});
        } catch (Exception ex) {
            log.error("查询${table.functionName}出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }

    /**
     * 查询${table.functionName}
     *
     * @param ${classNameLower} ${table.functionName}页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author ${table.codeAuthor}
     * @since [${table.codeVersion}]
     * @version [${table.codeVersion}, ${now?string('yyyy-MM-dd')}]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询${table.functionName}列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "queryForTree", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult queryForTree(QueryParams<${className}> queryParams) {
        JsonResult jr = new JsonResult();
        try {
            queryParams.setOrderColumn("inner_code");
            List<${className}> list = ${classNameLower}Service.queryForList(queryParams);
            jr.setData(list);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询${table.functionName}出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 新增${table.functionName}
     * 
     * @param entity ${table.functionName}页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author ${table.codeAuthor}
     * @since [${table.codeVersion}]
     * @version [${table.codeVersion}, ${now?string('yyyy-MM-dd')}]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "新增${table.functionName}")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "save", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult save${className}(${className} entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = ${classNameLower}Service.save(entity);
            jr.setResult(res);
            log.debug("新增${table.functionName}成功【{}】", entity);
        } catch (Exception ex) {
            log.error("新增${table.functionName}出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 删除${table.functionName}
     * 
     * @param id ${table.functionName}页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author ${table.codeAuthor}
     * @since [${table.codeVersion}]
     * @version [${table.codeVersion}, ${now?string('yyyy-MM-dd')}]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "删除${table.functionName}")
    @RequiresPermissions(menuCode + ":delete")
    @RequestMapping(value = "delete/{id}", method = { RequestMethod.DELETE })
    @ResponseBody
    public JsonResult delete(@PathVariable String id, HttpServletRequest request){
        JsonResult jr = new JsonResult();
        try {
            log.debug("删除${table.functionName}id={}",id);
            IResult codeMsg = ${classNameLower}Service.deleteByIds(id);
            jr.setResult(codeMsg);
        } catch (Exception ex) {
            log.error("删除${table.functionName}出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 修改${table.functionName}
     * 
     * @param entity ${table.functionName}页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author ${table.codeAuthor}
     * @since [${table.codeVersion}]
     * @version [${table.codeVersion}, ${now?string('yyyy-MM-dd')}]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改${table.functionName}")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "update", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult update${className}(${className} entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = ${classNameLower}Service.update(entity);
            jr.setResult(res);
            log.debug("修改${table.functionName}成功【{}】", entity);
        } catch (Exception ex) {
            log.error("修改${table.functionName}出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改部门")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "batchUpdate", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult batchUpdate${className}(@RequestBody List<${className}> ${classNameLower}s) {
        JsonResult jr = new JsonResult();
        try {
                IResult res = ${classNameLower}Service.batchUpdate${className}(${classNameLower}s);
                jr.setResult(res);
                log.debug("修改${table.functionName}成功【{}】", ${classNameLower}s);
            } catch (Exception ex) {
                log.error("修改${table.functionName}出错", ex);
                jr.setResult(ResultCode.ERROR);
            }
        return jr;
    }
    
    /**
     * 去查看${table.functionName}
     * 
     * @param id ${table.functionName}页面表单对象唯一标识
     * @return 结果视图
     * @author ${table.codeAuthor}
     * @since [${table.codeVersion}]
     * @version [${table.codeVersion}, ${now?string('yyyy-MM-dd')}]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看${table.functionName}详情")
    @RequiresPermissions(menuCode + ":view")
    @RequestMapping(value = "info/{id}", method = { RequestMethod.GET })
    @ResponseBody
    public JsonResult info(@PathVariable Long id) {
        JsonResult jr = new JsonResult();
        try {
            jr.setData(${classNameLower}Service.getById(id));
        } catch (Exception ex) {
            log.error("查看${table.functionName}出错", ex);
        }
        return jr;
    }
    
}
